package com.small.commom;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class HttpUtil { 
	
	
	
	
	/***
	 * http Get ȡ�������
	 * @param _url
	 * @return
	 * @throws IOException 
	 * @throws  
	 */
    public static String CallMethod(String _url) throws Exception
    {
        String result = "";
        InputStream is = null;
        HttpGet httpRequest=new HttpGet(_url);
        HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
        if(httpResponse.getStatusLine().getStatusCode()==200)
        { 
        	is = httpResponse.getEntity().getContent();
        	byte[] data = new byte[1024];
        	int n = -1;
        	ByteArrayBuffer buf = new ByteArrayBuffer(10*1024);
        	while ((n = is.read(data)) != -1)
        		buf.append(data, 0, n);  
        	result= new String(buf.toByteArray(), HTTP.UTF_8);
        	is.close();
        	return result;
        }
        else
        {
        	Log.v("tip==", "error response code");
        }
        return null;
    }
    
    /***
     * http postȡ�������
     * @param _url
     * @param parem ������
     * @return
     */
    public static String CallMethodPost(String _url,Parm[] parms)
    {
    	String result = "";
        InputStream is = null;
        HttpPost httpRequest=new HttpPost(_url);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(parms.length);
        if (parms != null)
		{
			for(Parm parm:parms)
			{
			    nameValuePairs.add(new BasicNameValuePair(parm.getKey().toString(), parm.getVal().toString()));
			}
		}
        try {
        		httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
                HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
                if(httpResponse.getStatusLine().getStatusCode()==200)
                { 
                    is = httpResponse.getEntity().getContent();
                    byte[] data = new byte[1024];
                    int n = -1;
                    ByteArrayBuffer buf = new ByteArrayBuffer(10*1024);
                    while ((n = is.read(data)) != -1)
                        buf.append(data, 0, n);  
                    result= new String(buf.toByteArray(), HTTP.UTF_8);
                    is.close();
                    return result;
                }
                else
                {
                    Log.v("tip==", "error response code");
                }
        } 
        catch (Exception e)
        {
            Log.e("error==", ""+e.getMessage());
        }
        return null;
    }
    
    /***
     * http Postȡ������� ����json����
     */
    public static Result CallMethodPostForResult(String _url,Parm[] parm){
    	return GetArray(CallMethodPost(_url, parm));
    }
    /***
     * http Getȡ������� ����json����
     * @param _url
     * @return
     * @throws Exception 
     */
    public static Result CallMethodForResult(String _url) throws Exception{
    	return GetArray(CallMethod(_url));
    }
    
    public static Result GetArray(String obj)
	{
		try
		{
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			JSONArray jsonArray = new JSONArray(obj);
			Map<String,Object> map = null;
			for(int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject item = jsonArray.getJSONObject(i);
				Iterator<?> keys = item.keys();
				map = new HashMap<String,Object>();
				while (keys.hasNext())
				{
					String key = (String)keys.next();
					map.put(key, item.getString(key));
				}
				list.add(map);
			}
			Result result = new Result();
			result.setResult(list);
			return result;
		}
		catch(Exception e)
		{
			return null;
		}
	}
    public static JSONObject toJsonObject(String json){
		JSONObject object;
		try {
			object=new JSONObject(json);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return object;
	}
   
}
